import { BaseDeployPanel } from "./BaseDeployPanel";
import { Language } from "../../../../../../extend/language/Language";
import { Cache } from "../../../../cache/Cache";
import { EActiveType } from "../../../../../../Message/Public/EActiveType";
import { ItemData } from "../../../../resource/info/ItemData";
import { ShopConfig } from "../../../../resource/ShopConfig";
import { Dispatcher } from "../../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../../events/DataEvent";
import { EventName } from "../../../../mvc/EventName";
import { NetDispatcher } from "../../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../../mvc/ServerCommand";
import { GlobalClass } from "../../../../../../com/mui/core/GlobalClass";
import { ItemConfig } from "../../../../resource/ItemConfig";
import { JSON } from "../../../../../../com/mui/serialization/json/JSON";
import { EquipmentModuleConst } from "../../data/EquipmentModuleConst";
import { ECategory } from "../../../../../../Message/Public/ECategory";
import { EItemUpdateResult } from "../../../../../../Message/Game/EItemUpdateResult";
import { MsgManager } from "../../../../manager/MsgManager";
import { DeployPanelItem } from "../DeployPanelItem";
import { FilterConst } from "../../../../../component/gconst/FilterConst";
import { EquipUpdateParams } from "../../data/EquipUpdateParams";
type int = number;
//class RemoveJewelPanel
    
    export  class RemoveJewelPanel extends BaseDeployPanel
    {
        constructor()
        {
            this.label_material = Language.getString(60242);
            this.posData = [{"x": 157, "y": 110, "label": Language.getString(60239), "canRemove": true, "noBoarder": false, "tooltip":""}, {"x": 33, "y": 180, "label":"", "canSelect": true, "noBoarder": true, "isJewel": true, "tooltip":""}, {"x": 82, "y": 180, "label":"", "canSelect": true, "noBoarder": true, "isJewel": true, "tooltip":""}, {"x": 131, "y": 180, "label":"", "canSelect": true, "noBoarder": true, "isJewel": true, "tooltip":""}, {"x": 180, "y": 180, "label":"", "canSelect": true, "noBoarder": true, "isJewel": true, "tooltip": Language.getString(61029)}, {"x": 229, "y": 180, "label":"", "canSelect": true, "noBoarder": true, "isJewel": true, "tooltip": Language.getString(90289)}, {"x": 278, "y": 180, "label":"", "canSelect": true, "noBoarder": true, "isJewel": true, "tooltip": Language.getString(74102)}, {"x": 33, "y": 230, "label": this.label_material, "noBoarder": true, "showName": true, "tooltip":""}, {"x": 82, "y": 230, "label": this.label_material, "noBoarder": true, "showName": true, "tooltip":""}, {"x": 131, "y": 230, "label": this.label_material, "noBoarder": true, "showName": true, "tooltip":""}, {"x": 180, "y": 230, "label": this.label_material, "noBoarder": true, "showName": true, "tooltip":""}, {"x": 229, "y": 230, "label": this.label_material, "noBoarder": true, "showName": true, "tooltip":""}, {"x": 278, "y": 230, "label": this.label_material, "noBoarder": true, "showName": true, "tooltip":""}];
            this.buttonLabel = Language.getString(60243);
            super();
            return;
        }

        /* internal  */isInFreeRemoveActiveTime(): Boolean
        {
            return Cache.instance.active.isActiveInEffect(EActiveType._EactiveTypeFreeRemoveJewel);
        }

        /* internal  */autoBuyMaterial(arg1: ItemData): void
        {
            var loc1=ShopConfig.instance.getShopItemInfoByCode(arg1.itemCode);
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallBuyItem, {"shopItemInfo": loc1, "amount": 1, "callBack": this.onItemBuySuccess}));
            return;
        }

        /* internal  */onItemBuySuccess(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1): void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            this.sendMaterialToController();
            this.onButtonClick();
            return;
        }

        /* internal  */updateHoles(): void
        {
            var loc1=0;
            var loc3=0;
            var loc2=allItem[0].itemData;
            if (loc2) 
            {
                loc1 = 1;
                while (loc1 <= 6) 
                {
                    loc3 = loc2.itemExInfo["hole" + loc1];
                    this["hole" + loc1] = loc3;
                    if (loc3 == 0) 
                    {
                        allItem[loc1].itemData = null;
                        allItem[loc1].source = GlobalClass.getBitmap("NoHole");
                        allItem[loc1 + 6].itemData = null;
                        allItem[loc1 + 6].enabled = true;
                    }
                    else if (loc3 == 1) 
                    {
                        allItem[loc1].itemData = null;
                        allItem[loc1].source = null;
                        allItem[loc1 + 6].itemData = null;
                        allItem[loc1 + 6].enabled = true;
                    }
                    else 
                    {
                        allItem[loc1].itemData = new ItemData(loc3);
                        if (this._currentSelectedHole == 0) 
                        {
                            this._currentSelectedHole = loc1;
                            allItem[loc1].selected = true;
                            this.sendMaterialToController();
                        }
                    }
                    ++loc1;
                }
            }
            else 
            {
                loc1 = 1;
                while (loc1 <= 6) 
                {
                    allItem[loc1].itemData = null;
                    allItem[loc1].enabled = true;
                    allItem[loc1 + 6].itemData = null;
                    allItem[loc1 + 6].enabled = true;
                    ++loc1;
                }
            }
            return;
        }

        protected /* override */ updateStatus(): void
        {
            var loc4=null;
            if (!this.updateConfigInfo) 
            {
                loc4 = ItemConfig.instance.getUpdateInfo(ItemConfig.RemoveJewel, 1)["condition"];
                this.updateConfigInfo = JSON.deserialize(loc4);
            }
            var loc1=0;
            var loc2=0;
            var loc3=1;
            while (loc3 <= 6) 
            {
                if (this["hole" + loc3] > 1 && allItem[loc3].selected) 
                {
                    ++loc1;
                }
                ++loc3;
            }
            if (this.updateConfigInfo) 
            {
                loc2 = loc1 * this.updateConfigInfo.cost;
            }
            else 
            {
                loc2 = 0;
            }
            if (_cost != loc2) 
            {
                _cost = loc2;
                dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CostChange, _cost, false, true));
            }
            confirmButton.enabled = true;
            return;
        }

        public /* override */ clear(): void
        {
            clearAllItem();
            this._currentSelectedHole = 0;
            allItem[1].selected = false;
            allItem[2].selected = false;
            allItem[3].selected = false;
            allItem[4].selected = false;
            allItem[5].selected = false;
            allItem[6].selected = false;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [], false, true));
            return;
        }

        public /* override */ setAutoChooseMaterial(arg1: Object): void
        {
            var loc1=arg1.material as Array<any>;
            var loc2=0;
            while (loc2 < loc1.length) 
            {
                if (loc1[loc2].material) 
                {
                    allItem[7 + loc2].itemData = loc1[loc2].material;
                    allItem[7 + loc2].enabled = loc1[loc2].isEnough;
                }
                if (allItem[7 + loc2].itemData == null) 
                {
                    allItem[1 + loc2].selected = false;
                }
                ++loc2;
            }
            this.updateStatus();
            return;
        }

        public /* override */ putInItem(arg1: ItemData, arg2: Boolean=false, arg3=null): void
        {
            if (arg1.category == ECategory._ECategoryEquip || arg1.category == ECategory._ECategoryPetEquip) 
            {
                this.clear();
                allItem[0].itemData = arg1;
                this.currentEquipmentData = arg1;
                this.updateHoles();
                this.updateStatus();
                this.updateTips();
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedEquimentChange, [arg1], false, true));
            }
            return;
        }

        /* internal  */updateTips(): void
        {
            var loc1=allItem[0].itemData;
            if (loc1 && loc1.category == ECategory._ECategoryPetEquip) 
            {
                allItem[4].toolTipData = Language.getString(61360);
                allItem[5].toolTipData = Language.getString(61361);
                allItem[6].toolTipData = Language.getString(79003);
            }
            else if (loc1 && loc1.category == ECategory._ECategoryEquip) 
            {
                allItem[4].toolTipData = Language.getString(61029);
                allItem[5].toolTipData = Language.getString(90289);
                allItem[6].toolTipData = Language.getString(74102);
            }
            else 
            {
                allItem[4].toolTipData = "";
                allItem[5].toolTipData = "";
                allItem[6].toolTipData = "";
            }
            return;
        }

        public moveItemOutByIndex(arg1: int): void
        {
            if (arg1 == 0) 
            {
                this.clear();
                this.updateHoles();
                this.updateTips();
            }
            this.updateStatus();
            return;
        }

        public /* override */ onUpdateResult(arg1: int, arg2: Boolean, arg3, arg4: Array<any>): void
        {
            if (arg1 != EItemUpdateResult._EItemUpdateResultSuccess) 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(60247));
            }
            else 
            {
                this.clear();
                MsgManager.showRollTipsMsg(Language.getString(60246));
            }
            this._resultPlayerItem = arg3;
            if (!this._delayTimer) 
            {
                this._delayTimer = new Object /* flash.utils.Timer */(500, 1);
                this._delayTimer.addEventListener(flash.events.TimerEvent.TIMER_COMPLETE, this.onDelayTimerComplete);
            }
            this._delayTimer.start();
            return;
        }

        /* internal  */onDelayTimerComplete(arg1: Object /* flash.events.TimerEvent */): void
        {
            if (this._delayTimer) 
            {
                this._delayTimer.removeEventListener(flash.events.TimerEvent.TIMER_COMPLETE, this.onDelayTimerComplete);
                this._delayTimer = null;
            }
            this.putInItem(new ItemData(this._resultPlayerItem));
            return;
        }

        protected /* override */ createChildren(): void
        {
            var loc2=null;
            var loc3=null;
            super.createChildren();
            var loc1=0;
            while (loc1 < this.posData.length) 
            {
                loc2 = this.posData[loc1];
                loc3 = new DeployPanelItem(loc1, loc2.noBoarder, loc2.isJewel, loc2.tooltip, loc2.showName);
                loc3.posIndex = loc1;
                loc3.x = loc2.x;
                loc3.y = loc2.y;
                loc3.label = loc2.label;
                if (loc2.canRemove) 
                {
                    loc3.mouseEnabled = true;
                    loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onItemClick);
                }
                else if (loc2.canSelect) 
                {
                    loc3.addEventListener(flash.events.MouseEvent.CLICK, this.onItemSelecteClick);
                }
                addChild(loc3);
                allItem.push(loc3);
                ++loc1;
            }
            confirmButton.label = this.buttonLabel;
            confirmButton.x = 143;
            confirmButton.y = 297;
            confirmButton.textField.filters = [FilterConst.glowFilter];
            confirmButton.styleName = "ButtonSpecial";
            addChild(confirmButton);
            confirmButton.addEventListener(flash.events.MouseEvent.CLICK, this.onButtonClick);
            this.updateHoles();
            return;
        }

        /* internal  */onItemSelecteClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            if (loc1.itemData) 
            {
                loc1.selected = !loc1.selected;
            }
            if (!loc1.selected) 
            {
                allItem[6 + loc1.posIndex].itemData = null;
                allItem[6 + loc1.posIndex].enabled = true;
                this.updateStatus();
            }
            this.sendMaterialToController();
            return;
        }

        /* internal  */sendMaterialToController(): void
        {
            var loc1=[{"jewelData": allItem[1].selected ? allItem[1].itemData : null, "materialData": allItem[6].itemData, "isEnough": allItem[6].enabled}, {"jewelData": allItem[2].selected ? allItem[2].itemData : null, "materialData": allItem[7].itemData, "isEnough": allItem[7].enabled}, {"jewelData": allItem[3].selected ? allItem[3].itemData : null, "materialData": allItem[8].itemData, "isEnough": allItem[8].enabled}, {"jewelData": allItem[4].selected ? allItem[4].itemData : null, "materialData": allItem[9].itemData, "isEnough": allItem[9].enabled}, {"jewelData": allItem[5].selected ? allItem[5].itemData : null, "materialData": allItem[10].itemData, "isEnough": allItem[10].enabled}, {"jewelData": allItem[6].selected ? allItem[6].itemData : null, "materialData": allItem[11].itemData, "isEnough": allItem[11].enabled}];
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChoosedMaterialChange, loc1, false, true));
            return;
        }

        /* internal  */onItemClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=arg1.currentTarget as DeployPanelItem;
            if (loc1.itemData) 
            {
                this.moveItemOutByIndex(loc1.posIndex);
            }
            return;
        }

        /* internal  */onButtonClick(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            if (allItem[0].itemData == null) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60244));
                return;
            }
            var loc1=this.currentEquipmentData.uid;
            var loc2=this.currentEquipmentData.posType;
            var loc3;
            (loc3 = []).push(allItem[0].itemData.uid);
            var loc4=1;
            while (loc4 <= 6) 
            {
                if (allItem[loc4].itemData && allItem[loc4].selected && allItem[6 + loc4].itemData && (allItem[6 + loc4].enabled || this.isInFreeRemoveActiveTime())) 
                {
                    loc3.push(allItem[6 + loc4].itemData.uid);
                    loc3.push("hole" + loc4);
                }
                else 
                {
                    if (allItem[loc4].itemData && allItem[loc4].selected && allItem[6 + loc4].itemData && !allItem[6 + loc4].enabled && Cache.instance.equipment.isAutoBuy) 
                    {
                        this.autoBuyMaterial(allItem[6 + loc4].itemData);
                        return;
                    }
                    if (allItem[loc4].itemData && allItem[6 + loc4].itemData && !allItem[6 + loc4].enabled) 
                    {
                        this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_ChangeMaterialTabBarIndex, 0, false, true, true));
                    }
                }
                ++loc4;
            }
            if (loc3.length == 1) 
            {
                MsgManager.showRollTipsMsg(Language.getString(60245));
                return;
            }
            if (!isEnoughMoney) 
            {
                this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_CoinNotEnough, null, false, true, true));
                MsgManager.showRollTipsMsg(Language.getString(60121));
                return;
            }
            var loc5;
            (loc5 = new EquipUpdateParams()).operationId = this.operationId;
            loc5.autoUpdate = false;
            loc5.equipPosType = loc2;
            loc5.materials = loc3;
            this.dispatchEvent(new DataEvent(EquipmentModuleConst.Event_DoUpdateEvent1, loc5, false, true));
            return;
        }

        public /* var */currentEquipmentData: ItemData;

        /* internal */ /* var */operationId: int=4;

        /* internal */ /* var */label_material: string;

        /* internal */ /* var */posData: Array<any>;

        /* internal */ /* var */buttonLabel: string;

        /* internal */ /* var */hole1: int;

        /* internal */ /* var */hole2: int;

        /* internal */ /* var */hole3: int;

        /* internal */ /* var */hole4: int;

        /* internal */ /* var */hole5: int;

        /* internal */ /* var */hole6: int;

        /* internal */ /* var */updateConfigInfo: Object;

        /* internal */ /* var */_currentSelectedHole: int=0;

        /* internal */ /* var */_delayTimer: Object /* flash.utils.Timer */;

        /* internal */ /* var */_resultPlayerItem;
    }
